package top.hzy520.goods.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import top.hzy520.common.constants.Constants;
import top.hzy520.goods.dao.CateGoryMapper;
import top.hzy520.goods.dao.ProductMapper;
import top.hzy520.goods.dao.ProductSkuMapper;
import top.hzy520.goods.entity.*;
import top.hzy520.goods.vo.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 20:03 2018/6/5
 */
@Service
public class GoodsServiceImpl implements GoodsService {
   @Autowired
   private CateGoryMapper cateGoryMapper;

   @Autowired
   private ProductMapper productMapper;

   @Autowired
   private ProductSkuMapper productSkuMapper;
       /**
       *@Author: HouZeYu
       *@Description: 查询分类列表
       *@Date: 18:44 2018/6/6
       */
    @Override
    @Cacheable(cacheNames = Constants.CACHE_GOODS_CATEGORY)
    public List<CategoryVo> categoryList() {
        //用自关联查询拼装分类列表并缓存到redis中
        List<CategoryVo> categoryVoList=new ArrayList<>();
        List<CateGory> cateGoryList=cateGoryMapper.selectAll();
        //拼装返回参数
            for (int i=0; i<cateGoryList.size();i++){
                CategoryVo categoryParentVo=new CategoryVo();
                categoryParentVo.setId(cateGoryList.get(i).getId());
                categoryParentVo.setName(cateGoryList.get(i).getName());
                List<CateGory> cateGoryChildrenList=cateGoryList.get(i).getChildren();
                List<CategoryVo> categoryChildrenVoList=new ArrayList<>();
                for (int j=0;j<cateGoryChildrenList.size();j++){
                    CategoryVo categoryChildrenVo=new CategoryVo();
                    categoryChildrenVo.setId(cateGoryChildrenList.get(j).getId());
                    categoryChildrenVo.setName(cateGoryChildrenList.get(j).getName());
                    List<Product> productList=cateGoryChildrenList.get(j).getProductList();
                    List<ProductVo> productVoList=new ArrayList<>();
                    for (int k=0;k<productList.size();k++){
                        ProductVo productVo=new ProductVo();
                        productVo.setId(productList.get(k).getId());
                        productVo.setSpuName(productList.get(k).getSpuName());
                        productVo.setHeadImg(productList.get(k).getHeadImg());
                        productVo.setPrice(productList.get(k).getPrice());
                        productVo.setActivityPrice(productList.get(k).getActivityPrice());
                        productVoList.add(productVo);
                    }
                    categoryChildrenVo.setProductList(productVoList);
                    categoryChildrenVoList.add(categoryChildrenVo);
                }
                categoryParentVo.setChildren(categoryChildrenVoList);
                categoryVoList.add(categoryParentVo);
            }
        List<CategoryVo> categoryVoList1=categoryVoList;
        return categoryVoList;
    }
    /**
    *@Author: HouZeYu
    *@Description: 通过id查询该子分类下所有商品
    *@Date: 18:44 2018/6/6
    */
    @Cacheable(cacheNames = Constants.CACHE_GOODS_CATEGORY_DETAIL,key ="#id")
    @Override
    public List<ProductVo> goodsCategoryDetail(Long id) {
        //查询该分类下的所有商品
       List<Product> productList=productMapper.selectCateGoryDetailById(id);
       //拼装返回参数
       List<ProductVo> productVoList=new ArrayList<>();
       for (int i=0;i<productList.size();i++){
          ProductVo productVo=new ProductVo();
          productVo.setId(productList.get(i).getId());
          productVo.setSpuName(productList.get(i).getSpuName());
          productVo.setPrice(productList.get(i).getPrice());
          productVo.setActivityPrice(productList.get(i).getActivityPrice());
          productVo.setHeadImg(productList.get(i).getHeadImg());
          productVoList.add(productVo);
       }
        return productVoList;
    }
   /**
   *@Author: HouZeYu
   *@Description: 通过id查询该父分类下所有商品
   *@Date: 18:45 2018/6/6
   */
    @Override
    @Cacheable(cacheNames = Constants.CACHE_GOODS_CATEGORY_ALL,key = "#id")
    public List<ProductVo> categoryAllGoods(Long id) {
        //查询该分类下的所有商品
        List<Product> productList=productMapper.selectCateGoryAllGoodsById(id);
        //拼装返回参数
        List<ProductVo> productVoList=new ArrayList<>();
        for (int i=0;i<productList.size();i++){
            ProductVo productVo=new ProductVo();
            productVo.setId(productList.get(i).getId());
            productVo.setSpuName(productList.get(i).getSpuName());
            productVo.setPrice(productList.get(i).getPrice());
            productVo.setActivityPrice(productList.get(i).getActivityPrice());
            productVo.setHeadImg(productList.get(i).getHeadImg());
            productVoList.add(productVo);
        }
        return productVoList;
    }
      /**
      *@Author: HouZeYu
      *@Description: 商品详情
      *@Date: 20:15 2018/6/6
      */
    @Override
    public List<ProductSkuVo> goodsDetail(Long id) {
      List<ProductSku> skuList=productSkuMapper.selectGoodDetailById(id);
      List<ProductSkuVo> productSkuVoList=new ArrayList<>();
        for (int i=0;i<skuList.size();i++){
          ProductSkuVo productSkuVo=new ProductSkuVo();
          productSkuVo.setId(skuList.get(i).getId());
          productSkuVo.setSkuName(skuList.get(i).getSkuName());
          productSkuVo.setSkuPrice(skuList.get(i).getSkuPrice());
          productSkuVo.setActivityPrice(skuList.get(i).getActivityPrice());
          productSkuVo.setImgUrl(skuList.get(i).getImgUrl());
          List<SkuPropertyOption> skuPropertyVoList=skuList.get(i).getSkuPropertyOptionList();
          List<SkuPropertyVo> propertyVoList=new ArrayList<>();
          for (int j=0;j<skuPropertyVoList.size();j++){
             SkuPropertyVo skuPropertyVo=new SkuPropertyVo();
             skuPropertyVo.setPropertyName(skuPropertyVoList.get(j).getPropertyList().get(0).getPropertyName());
              List<PropertyOption> skuPropertyOptionList=skuPropertyVoList.get(j).getPropertyOptionList();
              skuPropertyVo.setPropertyOption(skuPropertyOptionList.get(0).getOptionName());
              propertyVoList.add(skuPropertyVo);
          }
            productSkuVo.setProperty(propertyVoList);
            productSkuVoList.add(productSkuVo);
        }
        return productSkuVoList;
    }
    /**
    *@Author: HouZeYu
    *@Description: 通过商品id查找商品
    *@Date: 16:15 2018/6/10
    */
    @Override
    public List<ProductSku> selectSku(Set<Long> longs) {
      List<ProductSku> productSkuList= productSkuMapper.selectSkuById(longs);
        return productSkuList;
    }
}
